package com.punai.customer.controller;

import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusFbFee;
import com.punai.customer.domain.CusVersionsDiscrepancy;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.request.CusRewriteCodeRequest;
import com.punai.customer.domain.vo.*;
import com.punai.customer.service.ICusFbFeeService;
import com.punai.customer.service.ICusVersionsDiscrepancyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 分包费用项Controller
 * 
 * @author zxw
 * @date 2022-12-09
 */
@RestController
@RequestMapping("/cus/fbFee")
public class CusFbFeeController extends BaseController
{
    @Autowired
    private ICusFbFeeService dmcFbFeeService;
    @Autowired
    private ICusVersionsDiscrepancyService cusVersionsDiscrepancyService;

    /**
     * 查询分包费用项列表
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusFbFee dmcFbFee)
    {
        if(StringUtils.isEmpty(dmcFbFee.getChapterId())){
            return new TableDataInfo();
        }
        return dmcFbFeeService.selectDmcFbFeeList(dmcFbFee,getCompanyId());
    }

    /**
     * 获取分包费用项详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcFbFeeService.selectDmcFbFeeById(id,getCompanyId()));
    }

    /**
     * 新增分包费用项Old
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:add')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.INSERT)
    @PostMapping("addOld")
    public AjaxResult addOld(@RequestBody SubContVo vo)
    {
        return toAjax(dmcFbFeeService.insertFbFee(vo,getCompanyId()));
    }

    /**
     * 新增分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:add')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddSubjectContVo vo)
    {
        return toAjax(dmcFbFeeService.insertFbFeeBySubject(vo,getCompanyId()));
    }



    /**
     * 修改分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:edit')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.UPDATE)
    @PutMapping("editOld")
    public AjaxResult editOld(@RequestBody FbFeeUpdateVo vo)
    {
        return toAjax(dmcFbFeeService.updateDmcFbFee(vo,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbFee:edit')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FbUpdateVo vo)
    {
        return toAjax(dmcFbFeeService.updateFbFee(vo,getCompanyId()));
    }


    /**
     * 删除分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:remove')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(dmcFbFeeService.deleteDmcFbFeeById(id,getCompanyId()));
    }


    /**
     * 修改分包费用项
     */
    @PreAuthorize("@ss.hasPermi('cus:fbFee:edit')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.UPDATE)
    @PutMapping("/changeGxLink")
    public AjaxResult changeGxLink(@RequestBody ChangeGxLinkVo vo)
    {
        return AjaxResult.success(dmcFbFeeService.changeGxLink(vo,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbFee:edit')")
    @Log(title = CusConstants.FB_NAME, businessType = BusinessType.UPDATE)
    @PutMapping("/moveFeeToNewChapter/{chapterId}/{ids}")
    public AjaxResult moveFeeToNewChapter(@PathVariable String chapterId,@PathVariable String[] ids)
    {
        return toAjax(dmcFbFeeService.moveFeeToNewChapter(chapterId,ids,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbFee:rewriteCode')")
    @Log(title = "成本费用项-重新编码", businessType = BusinessType.UPDATE)
    @PostMapping("/rewriteCode")
    public AjaxResult rewriteCode(@RequestBody CusRewriteCodeRequest codeRequest)
    {
        if(StringUtils.isEmpty(codeRequest.getSpecId())){
            return AjaxResult.error("专业获取失败");
        }
        return toAjax(dmcFbFeeService.rewriteCode(codeRequest));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbFee:publish')")
    @GetMapping( "/validateCode/{specId}")
    public AjaxResult validateCode(@PathVariable String specId)
    {
        return AjaxResult.success(dmcFbFeeService.validateCode(getCompanyId(),specId));
    }

    @GetMapping("/getContrastData")
    public TableDataInfo getDataLog(CusVersionsDiscrepancy cusVersionsDiscrepancy) {
        startPage();
        cusVersionsDiscrepancy.setCompanyId(getCompanyId());
        cusVersionsDiscrepancy.setPublishType(CusConstants.FB_TYPE);
        return getDataTable(cusVersionsDiscrepancyService.selectStaVersionsDiscrepancyList(cusVersionsDiscrepancy));
    }

    @GetMapping(value = "/getStaFbFee/{specId}/{id}")
    public AjaxResult getStaMaterialItem(@PathVariable("specId") String specId,@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcFbFeeService.getStaFbFeeById(id,specId,getCompanyId()));
    }
}
